/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @BuileGestureExt.java  2014年9月24日 下午3:50:18 - Guzhu
 * @author Guzhu
 * @email:muyi126@163.com
 * @version 1.0
 */

package com.android.tiango.baba.util;

import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;

import com.android.tiango.baba.util.GestureUtils.Screen;

public class  BuileGestureExt  {
    public static final int GESTURE_UP=0;
    public static final int GESTURE_DOWN=1;
    public static final int GESTURE_LEFT=2;
    public static final int GESTURE_RIGHT=3;
    private OnGestureResult onGestureResult;
    private Context mContext;
    public BuileGestureExt(Context c,OnGestureResult onGestureResult) {
        this.mContext=c;
        this.onGestureResult=onGestureResult;
        screen = GestureUtils.getScreenPix(c);
    }
    public GestureDetector Buile()
    {
        return new GestureDetector(mContext, onGestureListener);
    }
    
    private Screen screen;
    private GestureDetector.OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener(){

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
        	System.out.println("e1:"+e1+" e2:"+e2);
        	if(null==e1||null==e2){
        		return true;
        	}
            float x = e2.getX() - e1.getX();
            float y = e2.getY() - e1.getY();
            //限制必须得划过屏幕的1/4才能算划过
            float x_limit = screen.widthPixels / 4;
            float y_limit = screen.heightPixels / 4;
            float x_abs = Math.abs(x);
            float y_abs = Math.abs(y);
            if(x_abs >= y_abs){
                //gesture left or right
                if(x > x_limit || x < -x_limit){
                    if(x>0){
                        //right
                        doResult(GESTURE_RIGHT); 
                    }else if(x<=0){
                        //left
                        doResult(GESTURE_LEFT); 
                    }
                }
            }else{
                //gesture down or up
                if(y > y_limit || y < -y_limit){
                    if(y>0){
                        //down
                        doResult(GESTURE_DOWN); 
                    }else if(y<=0){
                        //up
                        doResult(GESTURE_UP); 
                    }
                }
            }
            return true;
        }
        
    };
    public void doResult(int result)
    {
        if(onGestureResult!=null)
        {
            onGestureResult.onGestureResult(result);
        }
    }
    
    public interface OnGestureResult
    {
        public void onGestureResult(int direction);
    }
}